/**
 *  Company    : Sysnet Pioneer Solutions
 *  License    : MIT License
 *  Document   : sysnet_pioneer-dom_manager.js
 *  Created on : Dec 2, 2011, 5:30:28 PM
 *  Author     : SERVER
 *  Template   : javascript library
 */

(function(){
    
    $.debug.dom_post = function(msg){
        
        con.log("dom_post => ",msg);
        
    }
    
    $.set.dom_post = {
        
        url:"?r=public/_unknown_"
        ,dataType:"json"
        ,success:fn
        
    }
    
    /**
     * DOM post - post a data inside the form.
     * @param arg1
     *      @arg1 - url
     *      @arg1 - mixins see overload 1
     *      
     * @overload 1 - accept arg1 and post data under form.
     *      @eg
     *      
     *      var arg = {
     *      
     *      firstname:"Darkarts"
     *      
     *      } // this means $(form).find("[name=firstname]").val(arg1.firstname);
     * 
     * @overload 2 - the value is coming from the server and aniticipates that the return value is json.
     *      @eg
     *      
     *      var arg1 = {
     *          
     *          url : "?r=person/list"
     *          
     *      }
     */
    
    $.fn.dom_post = function(arg1){
        var $this = $(this);
        var set = $.extend({},$.set.dom_post,arg1);
        var debug = $.debug.dom_post;
        
        // bind events here
            
           $this.bind("success",set.success);
            
        // !bind events here
        
        
        
        var overload = function(arg1){
            
            if(typeof(arg1) == "object" && arg1.url === undefined){return 1;}
            if(arg1.url !== undefined) return 2;
            
            return 0;
        }
             
        if(overload(arg1) == 1){
            
            debug("Overload 1");
            
            $.each(arg1, function(key,val){
                
                $("[name='"+key+"']").val(val);
                
            })
            
            return true;
        }
        else if(overload(arg1) == 2){
            
            var success = function(response){
                
                $this.trigger("success");
                $this.dom_post($.parseJSON(response));
                
            }
            
            arg1.success = success;// over ride success from the arg1            
            
            debug(arg1);
            $.ajax(arg1);
        }
    }
    
})(jQuery)